// 模型层 - 负责数据处理
class StudentModel {
    constructor() {
        this.students = [];
        this.selectedStudent = null;
        this.isRolling = false;
        this.loadFromLocalStorage();
    }

    // 从本地存储加载数据
    loadFromLocalStorage() {
        try {
            const savedStudents = localStorage.getItem('studentList');
            if (savedStudents) {
                this.students = JSON.parse(savedStudents);
            }
        } catch (error) {
            console.error('加载本地存储失败:', error);
        }
    }

    // 保存到本地存储
    saveToLocalStorage() {
        try {
            localStorage.setItem('studentList', JSON.stringify(this.students));
        } catch (error) {
            console.error('保存到本地存储失败:', error);
        }
    }

    // 添加学生
    addStudent(name) {
        if (name && !this.students.includes(name)) {
            this.students.push(name);
            this.saveToLocalStorage();
            return true;
        }
        return false;
    }

    // 批量添加学生
    addStudents(names) {
        const added = [];
        names.forEach(name => {
            if (name && !this.students.includes(name)) {
                this.students.push(name);
                added.push(name);
            }
        });
        this.saveToLocalStorage();
        return added;
    }

    // 删除学生
    removeStudent(name) {
        const index = this.students.indexOf(name);
        if (index > -1) {
            this.students.splice(index, 1);
            this.saveToLocalStorage();
            return true;
        }
        return false;
    }

    // 获取所有学生
    getAllStudents() {
        return [...this.students];
    }

    // 获取学生数量
    getStudentCount() {
        return this.students.length;
    }

    // 随机选择学生
    getRandomStudent() {
        if (this.students.length === 0) return null;
        const randomIndex = Math.floor(Math.random() * this.students.length);
        return this.students[randomIndex];
    }

    // 设置选中的学生
    setSelectedStudent(student) {
        this.selectedStudent = student;
    }

    // 获取选中的学生
    getSelectedStudent() {
        return this.selectedStudent;
    }

    // 设置滚动状态
    setRollingState(isRolling) {
        this.isRolling = isRolling;
    }

    // 获取滚动状态
    isCurrentlyRolling() {
        return this.isRolling;
    }

    // 清空所有学生
    clearAllStudents() {
        this.students = [];
        this.selectedStudent = null;
        this.saveToLocalStorage();
    }

    // 从Excel数据解析学生名单
    parseExcelData(data) {
        const students = [];
        
        // 遍历所有工作表
        Object.keys(data).forEach(sheetName => {
            const sheet = data[sheetName];
            const range = XLSX.utils.decode_range(sheet['!ref']);
            
            // 遍历所有单元格
            for (let row = range.s.r; row <= range.e.r; row++) {
                for (let col = range.s.c; col <= range.e.c; col++) {
                    const cellAddress = XLSX.utils.encode_cell({ r: row, c: col });
                    const cell = sheet[cellAddress];
                    
                    if (cell && cell.v) {
                        const value = String(cell.v).trim();
                        // 过滤掉明显不是姓名的数据
                        if (value.length >= 2 && value.length <= 10 && 
                            !/^\d+$/.test(value) && // 不是纯数字
                            !value.includes('@') && // 不包含邮箱符号
                            !/^(姓名|学号|班级|年级)/.test(value)) { // 不是表头
                            students.push(value);
                        }
                    }
                }
            }
        });
        
        // 去重
        return [...new Set(students)];
    }

    // 导出为Excel格式的数据
    exportToExcel() {
        const ws = XLSX.utils.aoa_to_sheet([
            ['学生姓名'],
            ...this.students.map(name => [name])
        ]);
        
        const wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, '学生名单');
        
        return wb;
    }
}